/**
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package spwodne;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.jdesktop.layout.GroupLayout;

import tools.ExcelExporter;
import wydruk.Drukarnia;

public class OWies extends JFrame {

    public OWies(java.util.List wsie, String name, int nrSp, Float stawka) {
        nazwa = (new StringBuilder()).append("Miejcowo\u015Bci nale\u017C\u0105ce do sp\363\u0142ki ").append(name).toString();
        lw = wsie;
        nrSpolki = nrSp;
        this.stawka = stawka.floatValue();
        mtk = new MTWsi(lw, nrSpolki, stawka.floatValue());
        mtk.refresh();
        initComponents();
        jTable1.setAutoCreateRowSorter(true);
    }

    private void initComponents() {
        jPanel1 = new JPanel();
        jScrollPane1 = new JScrollPane();
        jTable1 = new JTable(mtk);
        jButton1 = new JButton();
        jButton2 = new JButton();
        jButton3 = new JButton();
        jButton4 = new JButton();
        jLabel1 = new JLabel(nazwa);
        jButton5 = new JButton();
        jButton6 = new JButton();
        jButton7 = new JButton();
        jButton8 = new JButton();
        setDefaultCloseOperation(2);
        setTitle("Sp\363\u0142ki wodne - okno miejscowo\u015Bci");
        addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent evt) {
                formKeyPressed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jPanel1.setMinimumSize(new Dimension(800, 600));
        jTable1.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jTable1.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jButton1.setText("Dodaj");
        jButton1.setMinimumSize(new Dimension(0, 0));
        jButton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jButton2.setText("Usu\u0144");
        jButton2.setMinimumSize(new Dimension(0, 0));
        jButton2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jButton3.setText("Edytuj");
        jButton3.setMinimumSize(new Dimension(0, 0));
        jButton3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jButton4.setText("Otw\363rz");
        jButton4.setMinimumSize(new Dimension(0, 0));
        jButton4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jButton5.setText("Zamknij");
        jButton5.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jButton6.setText("Drukuj zestawienie zaleg\u0142o\u015Bci wp\u0142at");
        jButton6.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jButton7.setText("Drukuj");
        jButton7.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        jButton8.setText("Do Excela");
        jButton8.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
            final OWies this$0;
            

            {
                this$0 = OWies.this;
            //super();
            }
        });
        GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().add(26, 26, 26).add(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().add(jLabel1).addContainerGap()).add(jPanel1Layout.createSequentialGroup().add(jPanel1Layout.createParallelGroup(1).add(jScrollPane1, -2, 587, -2).add(jPanel1Layout.createSequentialGroup().add(jButton6).add(50, 50, 50).add(jButton8).add(jButton7))).add(54, 54, 54).add(jPanel1Layout.createParallelGroup(1).add(jButton2, -1, 92, 32767).add(2, jButton3, -1, 92, 32767).add(jPanel1Layout.createParallelGroup(1, false).add(jButton1, -1, -1, 32767).add(jButton4, -1, 92, 32767).add(2, jButton5))).add(41, 41, 41)))));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().add(jPanel1Layout.createParallelGroup(1).add(jPanel1Layout.createSequentialGroup().add(81, 81, 81).add(jButton1, -2, -1, -2).addPreferredGap(0).add(jButton3, -2, -1, -2).addPreferredGap(0).add(jButton2, -2, -1, -2).add(54, 54, 54).add(jButton4, -2, -1, -2)).add(jPanel1Layout.createSequentialGroup().addContainerGap().add(jLabel1).add(26, 26, 26).add(jScrollPane1, -2, 450, -2))).addPreferredGap(0, 60, 32767).add(jPanel1Layout.createParallelGroup(3).add(jButton5).add(jButton6).add(jButton8).add(jButton7)).addContainerGap()));
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(1).add(jPanel1, -2, -1, -2));
        layout.setVerticalGroup(layout.createParallelGroup(1).add(jPanel1, -2, -1, -2));
        pack();
    }

    private void jButton8ActionPerformed(ActionEvent evt) {
        fc.setSelectedFile(new File("excel" + File.separator + "Spolka_" + nrSpolki + ".xls"));

        int returnVal = fc.showSaveDialog(this);
        if (returnVal == 0) {
            File file = fc.getSelectedFile();

            try {
                ExcelExporter.exportTable(jTable1, file);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Błąd podczas eksportu tabeli do pliku XLS");
            }
        }

    }

    private void jButton7ActionPerformed(ActionEvent evt) {
        int[] numerki = jTable1.getSelectedRows();
        if (jTable1.getSelectedRowCount() > 0) {
            List<Klient> llk = new ArrayList<Klient>();
            for (int nr : numerki) {
                llk.addAll(((Wies) lw.get(nr)).getLKli());
            }
            OWydruk ow = new OWydruk(llk);
            ow.setVisible(true);
        }
    }

    private void jButton6ActionPerformed(ActionEvent evt) {
        if (jTable1.getSelectedRowCount() > 0) {
            Object[] options = {"Wszystkich", "Tylko niezerowych"};
            int n = JOptionPane.showOptionDialog(this, "Czy chcesz wydrukować wszystkich klientów zaznaczonych miejscowosci,\n" +
                    "czy tylko tych, którzy maja niezerowe zaległości?", "Wybierz", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    options,
                    options[0]);

            int[] numerki = jTable1.getSelectedRows();
            Drukarnia dd = new Drukarnia();
            try {
                List<Wies> lww = new ArrayList<Wies>();
                for (int nr : numerki) {
                    lww.add((Wies) lw.get(nr));
                }
                if(n==0)
                    dd.drukujWies(lww, "xml/wies_lista_klientow.xml",true);
                else dd.drukujWies(lww, "xml/wies_lista_klientow.xml",false);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    //Preview p = new Preview(new DokPodsumSp(nrSpolki - 1));
    //p.setVisible(true);
    }

    private void formKeyPressed(KeyEvent evt) {
        KeyEvent _tmp = evt;
        if (evt.getKeyCode() == 27) {
            dispose();
        }
    }

    private void jButton5ActionPerformed(ActionEvent evt) {
        dispose();
    }

    private void jButton4ActionPerformed(ActionEvent evt) {
        if (jTable1.getSelectedRowCount() > 0) {
            openKlienci(jTable1.getSelectedRow());
        }
    }

    private void jButton3ActionPerformed(ActionEvent evt) {
        int nr = jTable1.getSelectedRow();
        if (jTable1.getSelectedRowCount() > 0) {
            oews = new OEWsi(mtk, ((Wies) lw.get(nr)).getNaz(), nrSpolki, ((Wies) lw.get(nr)).id.intValue(), nr);
            oews.setVisible(true);
        }
    }

    private void jButton2ActionPerformed(ActionEvent evt) {
        if (jTable1.getSelectedRowCount() > 0) {
            int odp = JOptionPane.showConfirmDialog(this, "Czy jestes pewien, \u017Ce chcesz usun\u0105\u0107 wybran\u0105 miejscowo\u015B\u0107 i ca\u0142\u0105 jej zawarto\u015B\u0107?", "Potwierdzenie", 0);
            if (odp == 0) {
                mtk.delRow(jTable1.getSelectedRow());
            }
        }
    }

    private void jTable1MouseClicked(MouseEvent evt) {
        if (evt.getClickCount() == 2) {
            openKlienci(jTable1.getSelectedRow());
        }
    }

    private void jTable1KeyPressed(KeyEvent evt) {
        label0:
        {
            int key;
            label1:
            {
                key = evt.getKeyCode();
                if (jTable1.getSelectedRowCount() <= 0) {
                    break label0;
                }
                KeyEvent _tmp = evt;
                if (key != 10) {
                    KeyEvent _tmp1 = evt;
                    if (key != 32) {
                        break label1;
                    }
                }
                openKlienci(jTable1.getSelectedRow());
                break label0;
            }
            KeyEvent _tmp2 = evt;
            if (key == 27) {
                dispose();
            }
        }
    }

    private void jButton1ActionPerformed(ActionEvent evt) {
        oews = new OEWsi(mtk, nrSpolki, lw.size() + 1);
        oews.setVisible(true);
    }

    private void openKlienci(int nr) {
        ok = new OKlient(((Wies) lw.get(nr)).getLKli(), ((Wies) lw.get(nr)).getNaz(), nrSpolki, ((Wies) lw.get(nr)).id.intValue(), mtk);
        ok.setDefaultCloseOperation(2);
        ok.setVisible(true);
    }
    final JFileChooser fc = new JFileChooser();
    private MTWsi mtk;
    private java.util.List lw;
    private String nazwa;
    private OKlient ok;
    private int nrSpolki;
    private float stawka;
    private OEWsi oews;
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JButton jButton4;
    private JButton jButton5;
    private JButton jButton6;
    private JButton jButton7;
    private JButton jButton8;
    private JLabel jLabel1;
    private JPanel jPanel1;
    private JScrollPane jScrollPane1;
    private JTable jTable1;
}
